﻿using CongregacaoOs.Business;
using CongregacaoOs.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CongregacaoOs.WebForm.Cadastros
{
    public partial class Relatorios : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["TipoUsuario"].ToString() != "Regional")
                Response.Redirect("~/Cadastros/Home.aspx");

            if (Session["DadosGerais"] == null) Response.Redirect("~/Cadastros/Account/Login.aspx");

            Page.Form.DefaultButton = btnBuscar.UniqueID;

            if (!IsPostBack)
            {
                AtualizarGrieView();
                AtualizarDropRegionalFiltro();
            }
        }

        public void AtualizarGrieView()
        {
            using (ccOsBus bus = new ccOsBus())
            {

                ccOs ent = new ccOs();
                DateTime dataFiltro = DateTime.MinValue;
                string aTipoUsuario = "";

                GetServantFeatures1_Result dadosGerais = new GetServantFeatures1_Result();

                dadosGerais = (GetServantFeatures1_Result)Session["DadosGerais"];

                if (Session["TipoUsuario"] != null) aTipoUsuario = Session["TipoUsuario"].ToString();


                ent.idRegionalOut = dropRegionalFiltro.SelectedValue != "" ? Convert.ToInt32(dropRegionalFiltro.SelectedValue) : 0;
                ent.idDistritalOut = dropDistritalFiltro.SelectedValue != "" ? Convert.ToInt32(dropDistritalFiltro.SelectedValue) : 0;
                ent.idAdmOut = dropAdmFiltro.SelectedValue != "" ? Convert.ToInt32(dropAdmFiltro.SelectedValue) : 0;
                ent.idSetorOut = dropSetorFiltro.SelectedValue != "" ? Convert.ToInt32(dropSetorFiltro.SelectedValue) : 0;


                ent.codigo = txtCodigoBuscar.Text != "" ? txtCodigoBuscar.Text.Trim() : "";
                ent.dataCriacao = txtDataInicioBuscar.Text != "" ? Convert.ToDateTime(txtDataInicioBuscar.Text.Trim()) : DateTime.MinValue;

                ent.status = radioStatusFiltro.SelectedValue == "3" ? "" : radioStatusFiltro.SelectedValue;


                if (txtDatafimBuscar.Text != "")
                {
                    if (Convert.ToDateTime(txtDatafimBuscar.Text) >= DateTime.Today)
                    {
                        dataFiltro = Convert.ToDateTime(txtDatafimBuscar.Text);
                    }
                }

                gvOs.DataSource = bus.OsSelectFull(ent, dataFiltro);
                gvOs.DataBind();
            }

        }

        #region Filtros
        public void AtualizarDropRegionalFiltro()
        {
            using (ccRegionalBus bus = new ccRegionalBus())
            {
                ccRegional ent = new ccRegional();
                ent.idEstado = 0;
                ent.nome = "";
                ent.idPais = 0;
                dropRegionalFiltro.DataSource = bus.RegionalSelect(ent);
                dropRegionalFiltro.DataBind();
            }

        }

        public void AtualizarDropDistritalFiltro()
        {
            using (ccDistritalBus bus = new ccDistritalBus())
            {
                ccDistrital ent = new ccDistrital();

                if (dropRegionalFiltro.SelectedValue != "")
                {
                    ent.idRegional = Convert.ToInt32(dropRegionalFiltro.SelectedValue);
                    dropDistritalFiltro.DataSource = bus.DistritalSelect(ent);
                    dropDistritalFiltro.DataBind();
                }
                else
                {
                    dropDistritalFiltro.Items.Clear();
                    dropAdmFiltro.Items.Clear();
                    dropSetorFiltro.Items.Clear();
                }

            }
        }

        public void AtualizarDropAdmFiltro()
        {
            if (dropRegionalFiltro.SelectedValue != "" && dropDistritalFiltro.SelectedValue != "")
            {
                using (ccAdmBus bus = new ccAdmBus())
                {

                    ccAdm ent = new ccAdm();

                    ent.idRegional = dropRegionalFiltro.SelectedValue != "" ? Convert.ToInt32(dropRegionalFiltro.SelectedValue) : 0;
                    ent.idDistrital = dropDistritalFiltro.SelectedValue != "" ? Convert.ToInt32(dropDistritalFiltro.SelectedValue) : 0;

                    dropAdmFiltro.DataSource = bus.AdmSelect(ent);
                    dropAdmFiltro.DataBind();

                }

            }
            else
            {
                dropAdmFiltro.Items.Clear();
                dropSetorFiltro.Items.Clear();
            }
        }

        public void AtualizarDropSetorFiltro()
        {
            if (dropAdmFiltro.SelectedValue == "")
            {
                dropSetorFiltro.Items.Clear();
                return;
            }

            using (ccSetorBus bus = new ccSetorBus())
            {
                ccSetor ent = new ccSetor();

                if (dropAdmFiltro.SelectedValue != "")
                {
                    dropSetorFiltro.DataSource = bus.SetorSelectByAdm(Convert.ToInt32(dropAdmFiltro.SelectedValue));
                    dropSetorFiltro.DataBind();

                }
                else
                {
                    dropSetorFiltro.Items.Clear();
                }
            }
        }
        #endregion

        protected void gvOs_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton img = (e.Row.FindControl("imgEditar") as ImageButton);
                img.CommandArgument = e.Row.RowIndex.ToString();

                ImageButton imgForum = (e.Row.FindControl("imgForum") as ImageButton);
                imgForum.CommandArgument = e.Row.RowIndex.ToString();
            }
        }

        protected void gvOs_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            AtualizarGrieView();
            gvOs.PageIndex = e.NewPageIndex;
            gvOs.DataBind();
        }

        protected void dropRegionalFiltro_DataBound(object sender, EventArgs e)
        {
            dropRegionalFiltro.Items.Insert(0, new ListItem("Escolha a Regional...", String.Empty));
        }

        protected void dropRegionalFiltro_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarDropDistritalFiltro();
            AtualizarGrieView();
        }

        protected void dropDistritalFiltro_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarDropAdmFiltro();
            AtualizarGrieView();

        }

        protected void dropDistritalFiltro_DataBound(object sender, EventArgs e)
        {
            dropDistritalFiltro.Items.Insert(0, new ListItem("Escolha a Distrital...", String.Empty));
        }

        protected void dropAdmFiltro_DataBound(object sender, EventArgs e)
        {
            dropAdmFiltro.Items.Insert(0, new ListItem("Escolha a Administração...", String.Empty));
        }

        protected void dropAdmFiltro_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarDropSetorFiltro();
            AtualizarGrieView();
        }

        protected void dropSetorFiltro_DataBound(object sender, EventArgs e)
        {
            dropSetorFiltro.Items.Insert(0, new ListItem("Escolha o Setor...", String.Empty));
        }

    }
}